package com.test.pusher;

import android.app.Activity;
import android.hardware.Camera;
import android.view.SurfaceHolder;

public class VideoChannel implements CameraHelper.OnChangedSizeListener, Camera.PreviewCallback {

    private static final String TAG = "VideoChannel";

    public int CAMERA_ID = Camera.CameraInfo.CAMERA_FACING_FRONT;
    public final int WIDTH = 800;
    public final int HEIGHT = 480;
    public final int FPS = 25;
    public final int BITRATE = 800000;

    private final Pusher mPusher;
    private CameraHelper mCameraHelper;
    private boolean isLive;

    public VideoChannel(Activity activity, Pusher pusher) {
        mPusher = pusher;
        mCameraHelper = new CameraHelper(activity, CAMERA_ID, WIDTH, HEIGHT);
        mCameraHelper.setOnChangedSizeListener(this);
        mCameraHelper.setPreviewCallback(this);
    }

    public void startLive() {
        isLive = true;
    }

    public void stopLive() {
        isLive = false;
    }


    public void release() {
        mCameraHelper.stopPreview();
    }

    public void setPreviewDisplay(SurfaceHolder surfaceHolder) {
        mCameraHelper.setPreviewDisplay(surfaceHolder);
    }

    public void switchCamera() {
        mCameraHelper.switchCamera();
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        // 推送视频数据
        if(isLive){
            mPusher.pushVideoNative(data);
        }
    }

    @Override
    public void onChanged(int w, int h) {
        // 初始化视频编码器
        mPusher.initVideoEncoderNative(w, h, BITRATE, FPS);
    }
}
